package cn.tedu.property_management_system.service.Impl;

import cn.tedu.property_management_system.cache.repo.IRedisCache;
import cn.tedu.property_management_system.entity.House;
import cn.tedu.property_management_system.entity.Meter;

import cn.tedu.property_management_system.entity.VO.MeterVO;
import cn.tedu.property_management_system.entity.dto.MeterDTO;
import cn.tedu.property_management_system.entity.dto.MeterSO;
import cn.tedu.property_management_system.ex.ServiceException;
import cn.tedu.property_management_system.mapper.MeterMapper;
import cn.tedu.property_management_system.service.IMeterService;

import cn.tedu.property_management_system.utils.JsonPage;
import cn.tedu.property_management_system.utils.JsonResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.jar.JarEntry;


import static cn.tedu.property_management_system.utils.JsonResult.State.ERR_USERNAME;

@Service
@Slf4j
public class MeterServiceImpl implements IMeterService {
    @Autowired
    MeterMapper mapper;


    @Override
    public Long addMeter(MeterVO meterVO) {
        Meter meterHouse = mapper.getByHouseNumber(meterVO.getHouseNumber());
        Meter meter = mapper.getByTime(meterVO.getHouseNumber(), meterVO.getMeterReadingTime());

        if (meterHouse != null) {
            if (meter != null) {
                throw new ServiceException(ERR_USERNAME, "该房间今天已抄表");
            }
        }

        meter = new Meter();
        meter.setLastDegree(meterHouse.getCurrentDegree());

        BeanUtils.copyProperties(meterVO, meter);

        meter.setCurrentDegree(meterVO.getCurrentDegree());

        meter.setRegistrationTime(LocalDateTime.now());

        meter.setTime1(System.currentTimeMillis());

        if (meterHouse != null) {
            meter.setId(meterHouse.getId());

            mapper.update(meter);
            return meter.getId();
        }
        int rows = mapper.insert(meter);
        if (rows == 0) {
            throw new ServiceException(ERR_USERNAME, "登记抄表记录失败");
        }
        return meter.getId();
    }

    @Override
    public int deleteMeter(Long id) {
        if (id == null) {
            throw new SecurityException("没有请求参数");
        }
        int rows = mapper.delete(id);
        if (rows == 0) {
            throw new ServiceException(ERR_USERNAME, "删除失败");
        }
        return rows;
    }

    @Override
    public int deleteMeters(List<Long> ids) {
        for (Long id : ids) {
            deleteMeter(id);
        }
        return 1;
    }


    @Override
    public List<MeterSO> selectMeter(MeterDTO meterDTO) {

        List<MeterSO> meter = mapper.getByHouseType(meterDTO.getHouseNumber(), meterDTO.getExpenseType());

        if (meter == null) {
            throw new ServiceException(ERR_USERNAME, "房间相关信息不存在");
        }

        return meter;
    }

    @Override
    public List<MeterSO> selectListMeter() {
        String key = "meter";
        List<MeterSO> meterSOS = new ArrayList<>();


        meterSOS = mapper.select();
        if (meterSOS == null || meterSOS.size() < 1) {
            throw new ServiceException(ERR_USERNAME, "没有数据");
        }

        return meterSOS;
    }

    @Override
    public void initCache() {
        loadCache();
    }

    @Override
    public void rebuildCache() {
        // TODO 添加限制频繁重建缓存的策略
        loadCache();
    }

    @Override
    public List<String> selectHouse() {

        return mapper.getHouse();
    }

    @Override
    public void startCache() {
        loadCache();
    }


    @Autowired
    IRedisCache redisCache;

    private void loadCache() {


    }

    public JsonPage<MeterSO> pageSelect(Integer pageSize, Integer pageIndex){
        PageHelper.startPage(pageSize,pageIndex);
        List<MeterSO> list=mapper.select();
        return JsonPage.restPage(new PageInfo<>(list));
    }




}
